<?php

namespace App\Http\Controllers\Api\Wap\Goods;

use App\Http\Controllers\Common\ApiController;
use App\Models\Common\China;
use App\Models\Constant\Constant;
use App\Models\Goods\Goods;
use App\Models\Order\Order;
use App\Models\User\FlyOrder;
use App\Models\User\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class GoodsController extends ApiController{


    public function __construct() {
        $this->instance = new Goods();
    }

    public function login(Request $request) {
        $passport = $request->input('passport');
        $name = $request->input('username');
        $user = User::query()->where(compact('passport','name'))->first();
        if(empty($user)) return responseFail('用户名或密码！');

        return responseSuccess($user->toArray());
    }

    public function lst(Request $request) {

        $conditions = $request->input();
        $records = $this->instance->getLst($conditions)->get();
        return responseSuccess($records->toArray());
    }

    public function detail(Request $request) {
        $id = $request->input('id');
        $record = $this->instance->find($id);
        return responseSuccess($record->toArray());
    }

    public function exchange(Request $request) {
        $user_id = $request->input('userid');
        $goods_id = $request->input('goodsid');
        $address = $request->input('address');
        $tel = $request->input('tel');
        $exchange_num = $request->input('exchangenum');
        $beizhu = $request->input('beizhu');


        if(Order::query()->where(['user_id'=>$user_id,'goods_id'=>$goods_id,'exchange_num'=>$exchange_num,'status'=>Constant::ORDER_STATUS_WAIT])->exists()){
            return responseFail('该订单已存在！请勿重复下单！');
        }

        $user =  User::query()->find($user_id);
        $goods = Goods::query()->find($goods_id);

        //检查商品数量
        if($goods->num < $exchange_num)
            return responseFail('很遗憾，商品余额不够！');

        //检查积分是否充足
        $need_point = $goods->point * $exchange_num;
        if($user->point < $need_point)
            return responseFail('很遗憾，您的积分不够！');
        try {

            DB::beginTransaction();
            //减积分
            $user->diviPoint($need_point);

            //减商品数量
            $goods->num-=$exchange_num;
            $goods->save();

            //下订单
            $status = Constant::ORDER_STATUS_WAIT;
            Order::query()->create(compact('need_point','user_id','goods_id','address','tel','exchange_num','status','beizhu'));
            DB::commit();
            return responseSuccess();
        }catch (\Exception $e){
            DB::rollBack();
            $tip = $e->getFile().':'.$e->getLine().':'.$e->getMessage();
            return responseFail($tip);
        }
    }


    public function getUserOrder(Request $request) {
        $user_id = $request->input('user_id');
        $map = [1=>'待发货',2=>'已发货',3=>'已取消'];
        //兑换商品订单
        $records = Order::query()->where(compact('user_id'))->with(['getUser','getGoods'])
            ->orderByDesc('id')->get()->each(function($item) use($map) {
                $item->client_status_name = $map[$item->status]??'未知';
            });

        //航班号加分
        $recordFlies = FlyOrder::query()->where(['user_id'=>$user_id])->get()->each(function ($item) {
            $item->need_point = $item->status==1?'待定':$item->need_point;
        });
        $records = $records->merge($recordFlies)->sortByDesc('created_at')->values();

        return responseSuccess($records->toArray());
    }

    public function getChina() {
        $province_list = China::query()->where('pcode',0)->pluck('name','code')->toArray();
        $city_list = China::query()->whereIn('pcode',array_keys($province_list))->pluck('name','code')->toArray();
        $county_list = China::query()->whereIn('pcode',array_keys($city_list))->pluck('name','code')->toArray();
        return responseSuccess(compact('province_list','city_list','county_list'));
    }
}
